package com.ty.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ty.common.constant.ResultMsgEnum;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.R;
import com.ty.common.validator.group.SaveGroup;
import com.ty.common.validator.group.UpdateGroup;
import com.ty.skysystem.entity.TruckEntity;
import com.ty.skysystem.service.TruckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 车辆信息表: 用于存放和管理车辆信息 字段列: team_id => 对应表: truck_team
 *
 * @author tianyuzhu
 * @email 1659770218@QQ.COM
 * @date 2020-12-11 15:30:43
 */
@RestController
@RequestMapping("/truck")
@Api(tags = "车辆信息管理模块: 用于存放和管理车辆信息")
@Slf4j
public class TruckController {
  @Autowired private TruckService truckService;

  /** 列表 */
  @GetMapping("/list")
  @ApiOperation(value = "查询所有车辆信息", notes = "查询所有车辆信息", response = TruckEntity.class)
  public R list(@ApiParam(name = "参数", value = "params") @RequestParam Map<String, Object> params) {
    PageUtils page = truckService.queryPage(params);

    return R.ok().put(page);
  }

  /** 信息 */
  @GetMapping("/info/{truckId}")
  @ApiOperation(value = "查询单个车辆信息", notes = "查询单个车辆信息", response = TruckEntity.class)
  public R info(
      @ApiParam(name = "车辆ID,从URI获取", value = "truckId")
          @PathVariable(value = "truckId")
          Long truckId) {
    QueryWrapper<TruckEntity> queryWrapper = new QueryWrapper<>();
    queryWrapper.select(TruckEntity.class, e -> !"is_deleted".equals(e.getColumn()));
    queryWrapper.eq("truck_id", truckId);
    TruckEntity truck = this.truckService.getOne(queryWrapper);
    return R.ok().put(truck);
  }

  /** 保存 */
  @PostMapping("/save")
  @ApiOperation(value = "新增车辆", notes = "新增车辆")
  public R save(
      @ApiParam(name = "车辆实体类", value = "truck") @Validated(value = {SaveGroup.class}) @RequestBody
          TruckEntity truck) {
    return truckService.save(truck)
        ? R.ok(ResultMsgEnum.INSERT_OK.getMsg())
        : R.error(ResultMsgEnum.INSERT_ERROR.getMsg());
  }

  /** 修改 */
  @PatchMapping("/update")
  @ApiOperation(value = "修改车辆", notes = "修改车辆")
  public R update(
      @ApiParam(name = "车辆实体类", value = "truck")
          @Validated(value = {UpdateGroup.class})
          @RequestBody
          TruckEntity truck) {
    truck.setUpdateTime(new Date());
    return truckService.updateById(truck)
        ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
        : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 删除 */
  @DeleteMapping("/delete")
  @ApiOperation(value = "删除车辆(id数组, 逻辑删除)", notes = "删除车辆(id数组, 逻辑删除)")
  public R delete(@ApiParam(name = "ID数组", value = "truckIds") @RequestBody Long[] truckIds) {
    if (truckIds.length == 0) {
      return R.error("ID不能为空");
    }
    return truckService.removeByIds(Arrays.asList(truckIds))
        ? R.ok(ResultMsgEnum.DELETE_OK.getMsg())
        : R.ok(ResultMsgEnum.DELETE_ERROR.getMsg());
  }
}
